<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类型转换</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.declarations.html">« 类型声明</a></li>
      <li style="float: right;"><a href="language.variables.html">变量 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>类型转换</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.type-juggling" class="sect1">
 <h2 class="title">类型转换</h2>

 <p class="simpara">
  PHP 在变量声明时不需要定义类型。在这种情况下，变量的类型由存储的值决定。也就是说，如果
  <span class="type">string</span> 赋值给 <var class="varname">$var</var>，然后 <var class="varname">$var</var>
  的类型就是 <span class="type">string</span>。之后将 <span class="type">int</span> 值赋值给 
  <var class="varname">$var</var>，它将是 <span class="type">int</span> 类型。
 </p>

 <p class="para">
  PHP 可能会尝试在某些上下文中自动将值转换为另一种类型。不同的上下文有：
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">Numeric</span>
   </li>
   <li class="listitem">
    <span class="simpara">String</span>
   </li>
   <li class="listitem">
    <span class="simpara">Logical</span>
   </li>
   <li class="listitem">
    <span class="simpara">Integral and string</span>
   </li>
   <li class="listitem">
    <span class="simpara">Comparative</span>
   </li>
   <li class="listitem">
    <span class="simpara">Function</span>
   </li>
  </ul>
 </p>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <span class="simpara">
   当值需要解释为不同类型时，值本身<em class="emphasis">不</em>会改变类型。
  </span>
 </p></blockquote>

 <p class="simpara">
  强制将变量当做某种变量来求值，参见<a href="language.types.type-juggling.html#language.types.typecasting" class="link">类型转换</a>一节。要更改变量的类型，请参阅
  <span class="function"><a href="function.settype.html" class="function">settype()</a></span> 函数。
 </p>

 <div class="sect2">
  <h3 class="title">Numeric contexts</h3>

  <p class="simpara">
   This is the context when using an
   <a href="language.operators.arithmetic.html" class="link">arithmetical operator</a>.
  </p>

  <p class="simpara">
   在这种情况下，如果任一运算对象是 <span class="type">float</span>（或者不能解释为 <span class="type">int</span>），则两个运算对象都将解释为
   <span class="type">float</span>，结果也将是 <span class="type">float</span>。否则，运算对象将解释为 <span class="type">int</span>，结果也将是
   <span class="type">int</span>。自 PHP 8.0.0 起，如果无法解释其中一个运算对象，则会抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">String contexts</h3>

  <p class="simpara">
   This is the context when using <span class="function"><a href="function.echo.html" class="function">echo</a></span>,
   <span class="function"><a href="function.print.html" class="function">print</a></span>,
   <a href="language.types.string.html#language.types.string.parsing" class="link">string interpolation</a>,
   or the string
   <a href="language.operators.string.html" class="link">concatenation operator</a>.
  </p>

  <p class="simpara">
   这种情况下，值将会解释为 <span class="type">string</span>。
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Logical contexts</h3>

  <p class="simpara">
   This is the context when using conditional statements, the
   <a href="language.operators.comparison.html#language.operators.comparison.ternary" class="link">ternary operator</a>,
   or a <a href="language.operators.logical.html" class="link">logical operator</a>.
  </p>

  <p class="simpara">
   在这种情况下，值将会解释为 <span class="type">bool</span>。
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Integral and string contexts</h3>

  <p class="simpara">
   This is the context when using a
   <a href="language.operators.bitwise.html" class="link">bitwise operators</a>.
  </p>

  <p class="simpara">
   在这种情况下，如果所有的运算对象都是 <span class="type">string</span>，则结果也将是
   <span class="type">string</span>。否则运算对象将解释为 <span class="type">int</span>，结果也将是
   <span class="type">int</span>。如果其中一个运算对象无法解释，则会抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Comparative contexts</h3>

  <p class="simpara">
   This is the context when using a
   <a href="language.operators.comparison.html" class="link">comparison operator</a>.
  </p>

  <p class="simpara">
   在此上下文中发生的类型转换在比较多种类型<a href="language.operators.comparison.html#language.operators.comparison.types" class="link">表</a>中进行了说明。
  </p>
 </div>

 <div class="sect2">
  <h3 class="title">Function contexts</h3>

  <p class="simpara">
   This is the context when a value is passed to a typed parameter, property,
   or returned from a function which declares a return type.
  </p>

  <p class="para">
   在此上下文中，当激活（默认）强制类型模式时，只有标量值可以转为另外一个标量值。对于简单类型声明，行为如下所示：
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      声明 <span class="type">bool</span> 类型：值将解释为 <span class="type">bool</span>。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      声明 <span class="type">int</span> 类型：如果明确定义转换，则值将解释为 <span class="type">int</span>。例如，字符串是<a href="language.types.numeric-strings.html" class="link">数字</a>。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      声明 <span class="type">float</span> 类型：如果明确定义转换，则值将解释为 <span class="type">float</span>。例如，字符串是<a href="language.types.numeric-strings.html" class="link">数字</a>。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      声明 <span class="type">string</span> 类型：值将解释为 <span class="type">string</span>。
     </span>
    </li>
   </ul>

   如果类型声明是联合，查看有关<a href="language.types.declarations.html#language.types.declarations.union.coercive" class="link">强制类型与联合类型</a> 的章节。
  </p>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    自 PHP 8.1.0 起弃用<a href="functions.internal.html" class="link">内部函数</a>自动将
    <strong><code>null</code></strong> 转换为标量类型的行为。
   </p>
  </div>
 </div>

 <div class="sect2" id="language.types.typecasting">
  <h3 class="title">类型转换</h3>

  <p class="simpara">
   类型转换通过在值前面的括号中写入类型来将值转换指定的类型。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是&nbsp;int<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;(bool)&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$bar&nbsp;是&nbsp;bool<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="simpara">
   允许的转换是：
  </p>

  <ul class="simplelist">
   <li class="member"><code class="literal">(int)</code> ——转换为 <span class="type">int</span></li>
   <li class="member"><code class="literal">(bool)</code> ——转换为 <span class="type">bool</span></li>
   <li class="member"><code class="literal">(float)</code> ——转换为 <span class="type">float</span></li>
   <li class="member"><code class="literal">(string)</code> ——转换为 <span class="type">string</span></li>
   <li class="member"><code class="literal">(array)</code> ——转换为 <span class="type">array</span></li>
   <li class="member"><code class="literal">(object)</code> ——转换为 <span class="type">object</span></li>
   <li class="member"><code class="literal">(unset)</code> ——转换为 <span class="type">NULL</span></li>
  </ul>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <code class="literal">(integer)</code> 是 <code class="literal">(int)</code> 转换的别名。<code class="literal">(boolean)</code>
    是 <code class="literal">(bool)</code> 转换的别名。<code class="literal">(binary)</code> 是 <code class="literal">(string)</code>
    转换的别名。<code class="literal">(double)</code> 和 <code class="literal">(real)</code> 是 <code class="literal">(float)</code>
    转换的别名。这些转换不使用标准的类型名称，不推荐使用。
   </p>
  </p></blockquote>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    自 PHP 8.0.0 起弃用 <code class="literal">(real)</code> 转换别名。
   </p>
  </div>

  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    自 PHP 7.2.0 起弃用 <code class="literal">(unset)</code> 转换。注意
    <code class="literal">(unset)</code> 转换等同于将值 <span class="type">NULL</span> 
    通过赋值或者调用给变量。自 PHP 8.0.0 起移除 <code class="literal">unset</code> 转换。
   </p>
  </div>

  <div class="caution"><strong class="caution">警告</strong>
   <p class="simpara">
    向前兼容 <code class="literal">(binary)</code> 转换和 <code class="literal">b</code>
    前缀转换。注意 <code class="literal">(binary)</code> 转换和 <code class="literal">(string)</code>
    相同，但是这可能会改变且不应依赖。
   </p>
  </div>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在转换的括号内忽略空格。因此，以下两个转换是等价的：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;(int)&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;(&nbsp;int&nbsp;)&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </p></blockquote>

  <div class="informalexample">
   <p class="simpara">
    将文字 <span class="type">string</span> 和变量转换为二进制 <span class="type">string</span>：
   </p>

   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$binary&nbsp;</span><span style="color: #007700">=&nbsp;(binary)&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">b"binary&nbsp;string"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    除了将变量转换为 <span class="type">string</span> 之外，还可以将变量放在双引号内。
   </span>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是&nbsp;int<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$str&nbsp;是&nbsp;string<br /></span><span style="color: #0000BB">$fst&nbsp;</span><span style="color: #007700">=&nbsp;(string)&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$fst&nbsp;也是&nbsp;string<br /><br />//&nbsp;打印出&nbsp;"they&nbsp;are&nbsp;the&nbsp;same"<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$fst&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"they&nbsp;are&nbsp;the&nbsp;same"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p></blockquote>
   
  <p class="para">
   有时在类型之间转换时确切地会发生什么可能不是很明显。更多信息见如下不分：
   <ul class="simplelist">
    <li class="member"><a href="language.types.boolean.html#language.types.boolean.casting" class="link">转换为 bool</a></li>
    <li class="member"><a href="language.types.integer.html#language.types.integer.casting" class="link">转换为 int</a></li>
    <li class="member"><a href="language.types.float.html#language.types.float.casting" class="link">转换为 float</a></li>
    <li class="member"><a href="language.types.string.html#language.types.string.casting" class="link">转换为 string</a></li>
    <li class="member"><a href="language.types.array.html#language.types.array.casting" class="link">转换为 array</a></li>
    <li class="member"><a href="language.types.object.html#language.types.object.casting" class="link">转换为 object</a></li>
    <li class="member"><a href="language.types.resource.html#language.types.resource.casting" class="link">转换为 resource</a></li>
    <li class="member"><a href="language.types.null.html#language.types.null.casting" class="link">转换为 NULL</a></li>
    <li class="member"><a href="types.comparisons.html" class="link">类型比较表</a></li>
   </ul>
  </p>
 </div>

 <div class="simplesect">
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    因为 PHP 的 <span class="type">string</span> 支持使用与 <span class="type">array</span>
    索引相同的语法，通过偏移量进行索引，所以以下示例适用于所有 PHP 版本：
   </span>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'car'</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;是&nbsp;string<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;依然是&nbsp;string<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bar<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <span class="simpara">
    请查看章节标题为<a href="language.types.string.html#language.types.string.substr" class="link">存取和修改字符串中的字符</a>获取更多信息。
   </span>
  </p></blockquote>
 </div>

</div></div></div></body></html>